/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.pinot.spi.config.table;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.pinot.spi.config.BaseJsonConfig;


/**
 * The {@code QueryConfig} class contains the table-level query execution related configurations.
 */
public class QueryConfig extends BaseJsonConfig {
  // The timeout for the entire query execution in milliseconds. This value will be gathered on the broker side, and
  // passed to the servers within the broker request.
  // If the broker times out, it will stop waiting for more server responses and return the reduced server responses
  // gathered so far, and numServersResponded should be smaller than numServersQueried.
  // If the server times out, it will directly interrupt the query execution. The server response does not matter much
  // because by the time the server times out, the broker should already timed out and returned the response.
  private final Long _timeoutMs;

  // Table config override for disable Groovy broker property
  private final Boolean _disableGroovy;

  // Table config override for use approximate function property
  private final Boolean _useApproximateFunction;

  // This map configures the expressions to override in the query. In certain scenarios, users cannot control the
  // queries sent to pinot (e.g. queries are auto-generated by some other tools), then they can use this map to override
  // the expressions within the query to the desired ones (e.g. override transform function to derived column).
  private final Map<String, String> _expressionOverrideMap;

  // Indicates the maximum length of serialized response across all servers for a query. This value is equally
  // divided across all servers processing the query.
  private final Long _maxQueryResponseSizeBytes;

  // Indicates the maximum length of the serialized response per server for a query.
  private final Long _maxServerResponseSizeBytes;

  @JsonCreator
  public QueryConfig(@JsonProperty("timeoutMs") @Nullable Long timeoutMs,
      @JsonProperty("disableGroovy") @Nullable Boolean disableGroovy,
      @JsonProperty("useApproximateFunction") @Nullable Boolean useApproximateFunction,
      @JsonProperty("expressionOverrideMap") @Nullable Map<String, String> expressionOverrideMap,
      @JsonProperty("maxQueryResponseSizeBytes") @Nullable Long maxQueryResponseSizeBytes,
      @JsonProperty("maxServerResponseSizeBytes") @Nullable Long maxServerResponseSizeBytes) {
    Preconditions.checkArgument(timeoutMs == null || timeoutMs > 0, "Invalid 'timeoutMs': %s", timeoutMs);
    Preconditions.checkArgument(maxQueryResponseSizeBytes == null || maxQueryResponseSizeBytes > 0,
        "Invalid 'maxQueryResponseSizeBytes': %s", maxQueryResponseSizeBytes);
    Preconditions.checkArgument(maxServerResponseSizeBytes == null || maxServerResponseSizeBytes > 0,
        "Invalid 'maxServerResponseSizeBytes': %s", maxServerResponseSizeBytes);

    _timeoutMs = timeoutMs;
    _disableGroovy = disableGroovy;
    _useApproximateFunction = useApproximateFunction;
    _expressionOverrideMap = expressionOverrideMap;
    _maxQueryResponseSizeBytes = maxQueryResponseSizeBytes;
    _maxServerResponseSizeBytes = maxServerResponseSizeBytes;
  }

  @Nullable
  @JsonProperty("timeoutMs")
  public Long getTimeoutMs() {
    return _timeoutMs;
  }

  @Nullable
  @JsonProperty("disableGroovy")
  public Boolean getDisableGroovy() {
    return _disableGroovy;
  }

  @Nullable
  @JsonProperty("useApproximateFunction")
  public Boolean getUseApproximateFunction() {
    return _useApproximateFunction;
  }

  @Nullable
  @JsonProperty("expressionOverrideMap")
  public Map<String, String> getExpressionOverrideMap() {
    return _expressionOverrideMap;
  }

  @Nullable
  @JsonProperty("maxQueryResponseSizeBytes")
  public Long getMaxQueryResponseSizeBytes() {
    return _maxQueryResponseSizeBytes;
  }

  @Nullable
  @JsonProperty("maxServerResponseSizeBytes")
  public Long getMaxServerResponseSizeBytes() {
    return _maxServerResponseSizeBytes;
  }
}
